

$(document).ready(function()
{	
	$("a[class='buyGift']").bind("click",function() 
	{
		var giftID=$("#giftId").val();
		var giftName=$("#giftName").html();
		var giftPrice=$("#giftPrice").html();
                var giftPrompt = $('#giftPrompt').html();
		$.ajax(
		{
			type: 'get',
			url: edp_domain_url + 'ajax_log_info/?tiny=1',
			timeout: 5000,
			global:false,
			beforeSend: function()
			{
				$.blockUI(
				{ 
					message: '验证用户...',
					css: {padding:'5px',color:'#CC0000'}
				});
			},
			error: function()
			{
				$.blockUI(
				{ 
					message: '验证出错...',
					css: {padding:'5px',color:'#CC0000'}
				});
				$.unblockUI();
			},
		    success: function(data)
		    { 
				if(data==false)
				{   
					popupLogin($('#PopBox'),'兑换礼品');  
					$.blockUI(
					{ 
						message: $('#PopBox'),    
						css: 
						{ 
							padding:        0,
							margin:         0,
							top:            '30%', 
					        left:           '35%', 
							width:          '',
							textAlign:      '', 
							color:          '', 
							border:         '',
							backgroundColor:'',       				
							cursor:         'wait'				        
							}
					}); 	
				}
				else
				{
                                    $('#gift_name').html( giftName );
                                    $('#gift_price').html( giftPrice );
                                    if( giftPrompt.trim() != '' ){
                                         $('#giftNoteBox').show();
                                         $('#gift_prompt').html( giftPrompt );
                                    }
                                    else
                                        $('#giftNoteBox').hide();

                                    $('#giftNote').val('');

                                    BindEvent( "#exchangeGift input[tag=confirm_exchange]", "click", function(){sendExchangeRequest( giftID )} );
                                    BindEvent( "#exchangeGift input[tag=confirm_cancel]", "click", function(){UnblockUI();} );
                                    ShowModelDialog( $("#exchangeGift"), null );
				}
			}
		}); 
	});
});

function sendExchangeRequest( giftId, giftPrompt )
{
	$.ajax({
        type: 'post',
        url: edp_domain_url + '/ajax_gift/?tiny=1',
        data: 'gid=' + giftId + '&action=buy&giftNote=' + $('#giftNote').val(),
        timeout: 5000,
		global:false,
        beforeSend: function()
        {
			SimpleMessage( "正在处理礼品兑换..." );
		 
		},
		error: function(XMLHttpRequest, textStatus, errorThrown)
		{
			SimpleMessage( "错误，请稍后再试...", 2000 );
		},
        success: function( data, textStatus )
        {
                MessageBox( data, 'Message', {yes: function(){ location.reload();}} );
			//SimpleMessage(data, 4000, function(){location.reload();});
        }
	});
}
